<!DOCTYPE html>
<html>

<head>
    <title>注册登录页面</title>
    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>

<body class="hold-transition">
<div id="app">
<h1>欢迎来到图书系统注册登录页面</h1>
    <el-form ref="form" :model="account"  :rules="rules" label-width="80px">
        <!-- prop  rules :定义校验规则 -->
        <el-form-item label="用户名" prop="userName">
            <el-input v-model="account.userName"></el-input>
        </el-form-item>
            <el-form-item label="密码" prop="passWord">
                <el-input v-model="account.passWord" prefix-icon="el-icon-lock" type="password"></el-input>
            </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="primary" @click="register">注册</el-button>

    </el-form>
</div>
</body>

<!-- 引入组件库 -->

<script src="../js/vue.js"></script>

<script src="../plugins/elementui/index.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>

<script src="../js/axios-0.18.0.js"></script>


</html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册登录页</title>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            account: {
                userName: '',
                passWord: ''

            },
            //定义校验规则
            rules: {
                userName: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                passWord: [
                    {required: true, message: '请输入密码', trigger: 'blur'}
                ]
            }
        },
        methods: {
                //登录
                login() {
                    axios.post("/user/login", this.account  //map 对象传参
                       /* {params:{
                        userName:this.account.userName,
                        passWord:this.account.passWord
                        }}*/
                    ).then((res) => {
                            if (res.data.msg === "success") { //判断当前操作是否成功
                                //console.log(res.data.data.token)

                                    const valueJwt = res.data.data.token; // 提取Map对象
                                // 创建一个Axios实例

                                const instance = axios.create();

                                // 在每个请求中自动添加JWT令牌到请求头中
                                instance.interceptors.request.use(function (config) {
                                    // 在发送请求之前做些什么
                                    config.headers.Authorization = 'Bearer ' + valueJwt;
                                    return config;
                                }, function (error) {
                                    // 对请求错误做些什么
                                    return Promise.reject(error);
                                });
                                // 将JWT存储在sessionStorage中  application-----会话存储空间
                                sessionStorage.setItem('jwt', valueJwt);

                                    // 稍后，你可以从sessionStorage中获取JWT
                                    var jwt = sessionStorage.getItem('jwt');

                                    // console.log(jwt); // 输出获取到的值
                                    location.href = 'http://192.168.40.136:8080/pages/main.html'
                                    //location.href = 'http://localhost:8080/pages/main.html'


                                // alert("账号或密码错误！")
                            } else {
                                alert("账号或密码错误！")
                                console.log(res)
                            }
                        })

                },
                register() {
                       //发送异步请求
                        axios.post("/user/register", this.account).then((res) => {
                            if (res.data.msg === "success") { //判断当前操作是否成功
                                alert("注册成功！")
                            } else {
                                alert(res.data.msg+"注册失败    ！")
                            }
                        })
                }
        }
    })
</script>

<style>  /*  登录画面 */
    body {
        background-color: #f2f2f2;
        font-family: Arial, sans-serif;
    }

    h1 {
        text-align: center;
        color: #333333;
    }

    form {
        margin: 0 auto;
        width: 300px;
        background-color: #ffffff;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 0 10px #cccccc;
    }

    label {
        display: block;
        margin-bottom: 10px;
        color: #666666;
    }

    input[type="text"],
    input[type="passWord"] {
        width: 100%;
        padding: 10px;
        border: 1px solid #cccccc;
        border-radius: 5px;
        margin-bottom: 20px;
        box-sizing: border-box;
    }

    input[type="submit"] {
        background-color: #4CAF50;
        color: #ffffff;
        padding: 10px 20px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
    }

    input[type="submit"]:hover {
        background-color: #3e8e41;
    }
</style>